/**
 * Created by cchen on 2015/5/19.
 */

var mysql=require('../../common/utils/mysqlUtil')


exports.getBalance=function(filter,callback){
    var retailerName="";
    if(filter.name!==""&&filter.name!=="all"){
        retailerName=" && r.retailer_name like '%"+decodeURI(filter.name)+"%'"
    }
    var date=""
    if(typeof filter.fromDate!=='undefined' &&typeof filter.toDate!=='undefined' &&filter.fromDate!==''&&filter.toDate!==''){
         date="&& v.spending_time between "+filter.fromDate+" and "+filter.toDate
    }

    var status=""
    if(filter.status&&filter.status!==""){
        status="&& p.status="+filter.status
    }

    exports.mysqlSelect(
        "r.code, p.user_id, r.retailer_name, t.voucher_name, t.voucher_val, p.status, p.deal_price, p.retailer_price, v.spending_time, v.create_time, v.reward_source, p.close_time",
        "voucher_retailer_profile p, retailer r, voucher_user_bind v, voucher_type t",
        "p.retailer_id=r.id && p.voucher_user_bind_id=v.id && v.voucher_type_id=t.id "+retailerName+date+status,
        null,function(err,result){
            callback(err,result)
        })
}

exports.mysqlSelect=function(feilds, tables, where,sort,callback){
    var selectStr="select "+(feilds?feilds:"*")+" from "+tables+""+(where?" where "+where:"")+(sort?" "+sort:"");
    console.log(selectStr)
    mysql.query(selectStr,function(err,result){
        callback(err,result);
    });
}


exports.mysqlUpdate=function(table,obj,callback){
    mysql.query("update "+table+" set ? where id="+obj.id,obj,callback);
}